package servlet;

import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/user")
public class UserServlet extends HttpServlet {

    // 创建业务层对象  多态
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<User> users = userService.findAll();
//        req.setAttribute("users", users);
//        //转发
//        req.getRequestDispatcher("/list.jsp").forward(req, resp);
        //要与action相配合
       UserService userService = new UserServiceImpl();
       req.setCharacterEncoding("utf-8");//设置编码,避免乱码
       String action = req.getParameter("action");
       if("list".equals(action)){
           //接收参数
           String name= req.getParameter("name");
           String pass= req.getParameter("pass");
           User condition = new User(name,pass);
           //全查询
           List<User> users = userService.findAll(condition);
           //把数据放请求域
           req.setAttribute("users", users);
           req.setAttribute("condition", condition);
           //转发，在哪个页面显示数据
           req.getRequestDispatcher("/list.jsp").forward(req, resp);
       }
       else if("add".equals(action)){
           String name = req.getParameter("name");
           String pass = req.getParameter("pass");
         //  UserService userService = new UserServiceImpl(); 前面已经命名过了，不在重复命名
           int result=userService.addUser(new User(name,pass));//new User(name,pass) 是为了未来传递大量数据列时候方便
           if(result>0){
               resp.sendRedirect("user?action=list");
           }else {
               resp.sendRedirect("addUser.jsp");
           }
       }else if("delete".equals(action)){
           String sid = req.getParameter("id");
           Integer id=Integer.parseInt(sid);//转换把sid字符串类型转为int类型
           int result=userService.deleteUserById(id);//拿到页面传递的参数
           if(result>0){
               resp.sendRedirect("user?action=list");//重定向 再次查询， 新增的数据能表示出来
           }
       }else if("toupdate".equals(action)){
           String sid = req.getParameter("id");//写的id 对应  <a href="toupdate?id=${user.id}">修改</a> =前的值
           Integer id=Integer.parseInt(sid);//根据id 查询出数据，来唯一查到一条信息
           User user = userService.findById(id);//确保返回值，括号里的值没错，2
           //不能写重定向，要把数据存下来
           //将对象存储在作用域中
           req.setAttribute("user",user);//转发，更新到页面
           req.getRequestDispatcher("update.jsp").forward(req,resp);
       }else if("update".equals(action)){
           String sid = req.getParameter("id");
           Integer id=Integer.parseInt(sid);//转换把sid字符串类型转为int类型
           //拿到页面传递的参数
           String name = req.getParameter("name");//写的name需要与AddUserServlet里的name 对应
           String pass = req.getParameter("pass");
           int result=userService.updateUser(new User(id,name,pass));//new User(name,pass) 是为了未来传递大量数据列时候方便
           if(result>0){
               //重定向，跳转到user页面，("user")不能写成("/user")
               //重定向 再次查询 新增的数据能表示出来
               resp.sendRedirect("user?action=list");
           }else {
               resp.sendRedirect("addUser.jsp");//一般模拟不出来，要么成功，要么系统崩
           }
       }
    }
}

